package com.dzqc.fly.service.impl;

import com.dzqc.fly.db.bo.ClassBo;
import com.dzqc.fly.db.bo.SecretarySumBo;
import com.dzqc.fly.db.bo.StudentsFlyMsgBo;
import com.dzqc.fly.db.dao.SecretarySumDao;
import com.dzqc.fly.db.pojo.SecretarySumPojo;
import com.dzqc.fly.db.pojo.StudentsFlyMsgPojo;
import com.dzqc.fly.db.vo.SecretaryStudentsTimeVo;
import com.dzqc.fly.db.vo.SecretarySumVo;
import com.dzqc.fly.service.SecretarySumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hlp
 */
@Service
public class SecretarySumServiceImpl implements SecretarySumService {

    @Autowired
    private SecretarySumDao secretarySumDao;
    @Override
    public SecretarySumVo searchColleges(HashMap param) {
        //查询院系和班级
        List<SecretarySumPojo> pojoList = secretarySumDao.searchColleges(param);
        //数据格式转化
        SecretarySumVo vo=convert(pojoList.stream().distinct().collect(Collectors.toList()));

        return vo;
    }

    @Override
    public SecretaryStudentsTimeVo searchStudentsByTime(HashMap param) {
        //查询学生请假信息list
        List<StudentsFlyMsgPojo> pojoList = secretarySumDao.searchStudentFlyMsg(param);
        //转月份显示
        SecretaryStudentsTimeVo vo=convertStudentsByTime(pojoList);
        return vo;
    }

    private SecretarySumVo convert(List<SecretarySumPojo> pojoList){
        if(pojoList==null||pojoList.size()==0){
            return null;
        }
        SecretarySumVo secretarySumVo=new SecretarySumVo();
        List<SecretarySumBo> boList=new ArrayList<>();
        //按照院系id分组
        Map<String, List<SecretarySumPojo>> tempMap = pojoList.stream().collect(Collectors.groupingBy(SecretarySumPojo::getCollegeId));
        //按照key排序院系
        TreeMap<String, List<SecretarySumPojo>> result = tempMap.entrySet().stream()
                .sorted(Comparator.comparing(Map.Entry::getKey))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (k1, k2) -> k1, TreeMap::new));

        for (String key : result.keySet()) {
            List<SecretarySumPojo> tempList=result.get(key);
            //每个院系设置院系名
            SecretarySumBo bo=new SecretarySumBo();
            bo.setCollege(tempList.get(0).getCollege());
            bo.setCollegeId(tempList.get(0).getCollegeId());
            //每个院系设置院系名的班级
            List<ClassBo> classBos=new ArrayList<>();
            for(SecretarySumPojo pojo :tempList){
                ClassBo classBo=new ClassBo();
                classBo.setClassId(pojo.getClassId());
                classBo.setClassName(pojo.getClassName());
                classBos.add(classBo);
            }
            bo.setClassBos(classBos.stream().distinct().collect(Collectors.toList()));
            boList.add(bo);
        }
        secretarySumVo.setVo(boList);

        return secretarySumVo;
    }
    private SecretaryStudentsTimeVo convertStudentsByTime(List<StudentsFlyMsgPojo> pojoList){
        if(pojoList==null||pojoList.size()==0){
            return null;
        }
        SecretaryStudentsTimeVo secretaryStudentsTimeVo=new SecretaryStudentsTimeVo();
        List<StudentsFlyMsgBo> boList=new ArrayList<>();
        //按照月份对学生请假排序
        Map<String, List<StudentsFlyMsgPojo>> tempMap = pojoList.stream().collect(Collectors.groupingBy(StudentsFlyMsgPojo::getYearMonth));
        for (String key : tempMap.keySet()) {
            List<StudentsFlyMsgPojo> studentsFlyMsgPojos=tempMap.get(key);
            //设置月份
            StudentsFlyMsgBo bo=new StudentsFlyMsgBo();
            bo.setYearMonths(key);
            //每个月份设置请假学生信息
            bo.setStudentsFlyMsgPojos(studentsFlyMsgPojos);
            boList.add(bo);
        }
        secretaryStudentsTimeVo.setVo(boList);

        return secretaryStudentsTimeVo;
    }
}
